package com.kltn.zigo.data.loader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import android.widget.ListView;

import com.kltn.zigo.data.IP;
import com.kltn.zigo.process.JSONParser;

public class UserLoader {
	JSONParser jParser = new JSONParser(); 
    	
	private static String url_get_user = IP.get_IP() +  IP.get_path_service() + "get_user.php";
	
	 // JSON Node names
    public final String TAG_SUCCESS = "success";
    public final String TAG_USER = "user";
    public final String TAG_USER_ID = "user_id";
    public final String TAG_USER_ID_ZING = "user_id_zing";
    public final String TAG_USER_NAME_ZING = "user_name_zing";
    public final String TAG_USER_AVATAR_ZING = "user_avatar_zing";
    public final String TAG_USER_DISPLAY_NAME = "user_display_name";

    // products JSONArray
    JSONArray users = null;
    
    String user_id_zing;
    
    public UserLoader(String UserIdZing)
    {
    	user_id_zing = UserIdZing;
    }
    
    public HashMap<String, String> GetUser()
    {
    	HashMap<String, String> usersList = new HashMap<String,String>();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("user_id_zing", user_id_zing));
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_get_user, "GET", params);

        Log.d("User: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
            	users = json.getJSONArray(TAG_USER);

                // looping through All Products
                    JSONObject c = users.getJSONObject(0);

                    // Storing each json item in variable
                    String userId = c.getString(TAG_USER_ID);
                    String userIdZing = user_id_zing;
                    String userNameZing = c.getString(TAG_USER_NAME_ZING);
                    String userAvatarZing = c.getString(TAG_USER_AVATAR_ZING);
                    String userDisplayName = c.getString(TAG_USER_DISPLAY_NAME);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_USER_ID, userId);
                    map.put(TAG_USER_ID_ZING, userIdZing);
                    map.put(TAG_USER_NAME_ZING, userNameZing);
                    map.put(TAG_USER_AVATAR_ZING, userAvatarZing);
                    map.put(TAG_USER_DISPLAY_NAME, userDisplayName);

                    // adding HashList to ArrayList
                    usersList = map;
                
            } 
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return usersList;
    }
}
